<template>
  <div class="content">
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="query"
        class="demo-form-inline"
      >
        <el-form-item label="提交者">
          <el-input
            v-model="query.username"
            placeholder="请输入提交者姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="提交时间">
          <el-date-picker
            v-model="query.create_time"
            type="daterange"
            range-separator="到"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getTableInfo"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-row align="top" :gutter="20">
      <el-col :span="3">
        <el-card header="选择部门">
          <el-tree
            :data="departmentOption"
            node-key="id"
            default-expand-all
            check-on-click-node
            show-checkbox
            :expand-on-click-node="false"
            check-strictly
            :props="{ label: 'title', value: 'id' }"
            ref="treeRef"
            :highlight-current="true"
            @check-change="handleStaff"
          ></el-tree>
        </el-card>
      </el-col>
      <el-col :span="21">
        <el-card>
          <el-table :data="tableData" row-key="id" height="100%" stripe>
            <el-table-column type="index" label="#" fixed="left" />
            <el-table-column prop="title" label="标题" fixed="left" />
            <el-table-column
              prop="user.real_name"
              label="提交者"
              fixed="left"
            />
            <el-table-column
              prop="department.title"
              sortable
              label="所属部门"
            />
            <el-table-column prop="create_time" sortable label="提交时间" />
            <el-table-column label="操作" fixed="right" width="200">
              <template #default="scope">
                <el-button
                  type="primary"
                  link
                  icon="Edit"
                  @click="handleOpen(scope.row.id)"
                >
                  查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-divider border-style="none" />
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            background
            :current-page="curPage"
            :page-size="pageSize"
            layout="total, sizes,prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </el-card>
      </el-col>
    </el-row>
    <Drawer
      ref="reportDraw"
      @getTable="getTableInfo"
      :department="departmentOption"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import {
  deleteDepartment,
  getDepartmentOptionsApi,
} from "@/api/company/department";
import Drawer from "./component/reportDraw.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getReportAPi } from "@/api/company/examine";
const activeName = ref("0");
const reportDraw = ref<any>(null);
const query = reactive({
  department_id: [],
  username: "",
  create_time: [],
});
const total = ref(0);
watch(
  query,
  () => {
    getTableInfo();
  },
  { deep: true }
);
watch(activeName, (newVal) => {
  query.class_id = newVal;
});
const curPage = ref(1);
const pageSize = ref(20);
const departmentOption = ref<any[]>([]);
const tableData = ref([]);
const staffRef = ref<any>(null);
onMounted(() => {
  getDepartmentOptionsApi().then((res) => {
    departmentOption.value = res.data;
  });
  getTableInfo();
});
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
function getTableInfo() {
  getReportAPi({
    page: curPage.value,
    page_size: pageSize.value,
    ...query,
  }).then((res) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}
// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(id?: number) {
  reportDraw.value.dialogOpen(id || 0);
}

const handleStaff = (e: any, checked: boolean) => {
  if (checked) {
    query.department_id.push(e.id);
  } else {
    query.department_id = query.department_id.filter(
      (item: any) => item != e.id
    );
  }
};

defineExpose({
  getTableInfo,
});
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
